<template>
  <div>
    <el-dialog
      :visible.sync="dialog.add"
      :close-on-click-modal="false"
      title="添加"
      width="80%"
      append-to-body
      @closed="dialogClosed('addForm')"
    >
      <el-form ref="addForm" :model="form" label-width="120px" :rules="validateRules">
        <el-row>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="用户手机号" prop="userPhone">
              <el-input v-model="form.userPhone" placeholder="请输入用户手机号" disabled clearable />
              <el-button
                type="text"
                size="small"
                @click="showJoinSearch({
                searchDialog: 'UserMain'
              })"
              >选择用户手机号</el-button>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="昵称" prop="userNickname">
              <el-input v-model="form.userNickname" placeholder="请输入昵称" disabled clearable />
              <el-button
                type="text"
                size="small"
                @click="showJoinSearch({
                searchDialog: 'UserMain'
              })"
              >选择昵称</el-button>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="类目名称" prop="categoryTitle">
              <el-input v-model="form.categoryTitle" placeholder="请输入类目名称" disabled clearable />
              <el-button
                type="text"
                size="small"
                @click="showJoinSearch({
                searchDialog: 'GoodsCategoryMain'
              })"
              >选择类目名称</el-button>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="主体类型" prop="subjectType">
              <el-select
                v-model="form.subjectType"
                placeholder="请选择主体类型"
                clearable
                style="width: 100%;"
              >
                <el-option
                  v-for="item in selects.subjectTypeSelect"
                  :value="item.value"
                  :key="item.value"
                  :label="item.displayValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="店铺Logo" prop="logo">
              <UploadImage
                v-if="dialog.add"
                action="/goods-shop/admin/upload-img"
                formProp="logo"
                :fileList="[]"
                @imgUploadSuccess="imgUploadSuccess"
                @imgRemoveSuccess="imgRemoveSuccess"
              />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="店铺标题" prop="title">
              <el-input v-model="form.title" placeholder="请输入店铺标题" clearable />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="联系电话" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入联系电话" clearable />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
            <el-form-item label="店铺地址" prop="address">
              <el-input type="textarea" v-model="form.address" placeholder="请输入店铺地址" clearable />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="经纬度" prop="longitude">
              <el-button
                type="text"
                size="small"
                @click="showBaiduMap({
                searchDialog: 'UserMain'
              })"
              >选择经纬度 :</el-button>
              {{this.form.longitude}} - {{this.form.latitude}}
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
            <el-form-item label="店铺简介" prop="intro">
              <el-input type="textarea" v-model="form.intro" placeholder="请输入店铺简介" clearable />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="店铺等级" prop="level">
              <el-input-number
                v-model="form.level"
                placeholder="请输入店铺等级"
                clearable
                style="width: 100%;"
              />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="审核状态" prop="checkStatus">
              <el-select
                v-model="form.checkStatus"
                placeholder="请选择审核状态"
                clearable
                style="width: 100%;"
              >
                <el-option
                  v-for="item in selects.checkStatusSelect"
                  :value="item.value"
                  :key="item.value"
                  :label="item.displayValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
            <el-form-item label="审核描述" prop="checkDescription">
              <el-input
                type="textarea"
                v-model="form.checkDescription"
                placeholder="请输入审核描述"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="审核人手机号" prop="checkedUserPhone">
              <el-input v-model="form.checkedUserPhone" placeholder="请输入审核人手机号" disabled clearable />
              <el-button
                type="text"
                size="small"
                @click="showJoinSearch({
                searchDialog: 'UserMain'
              })"
              >选择审核人手机号</el-button>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="审核时间" prop="checkedTime">
              <el-date-picker
                type="datetime"
                v-model="form.checkedTime"
                placeholder="请输入审核时间"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss"
                style="width: 100%;"
                clearable
              ></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer">
        <el-button @click="dialog.add = false">取消</el-button>
        <el-button type="primary" @click="add" :loading="loading.add">添加</el-button>
      </div>
    </el-dialog>
    <el-dialog
      :visible.sync="dialog.edit"
      :close-on-click-modal="false"
      title="修改"
      width="80%"
      append-to-body
      @closed="dialogClosed('editForm')"
    >
      <el-form ref="editForm" :model="form" label-width="120px" :rules="validateRules">
        <el-row>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="用户手机号" prop="userPhone">
              <el-input v-model="form.userPhone" placeholder="请输入用户手机号" disabled clearable />
              <el-button
                type="text"
                size="small"
                @click="showJoinSearch({
                searchDialog: 'UserMain'
              })"
              >选择用户手机号</el-button>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="昵称" prop="userNickname">
              <el-input v-model="form.userNickname" placeholder="请输入昵称" disabled clearable />
              <el-button
                type="text"
                size="small"
                @click="showJoinSearch({
                searchDialog: 'UserMain'
              })"
              >选择昵称</el-button>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="类目名称" prop="categoryTitle">
              <el-input v-model="form.categoryTitle" placeholder="请输入类目名称" disabled clearable />
              <el-button
                type="text"
                size="small"
                @click="showJoinSearch({
                searchDialog: 'GoodsCategoryMain'
              })"
              >选择类目名称</el-button>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="主体类型" prop="subjectType">
              <el-select
                v-model="form.subjectType"
                placeholder="请选择主体类型"
                clearable
                style="width: 100%;"
              >
                <el-option
                  v-for="item in selects.subjectTypeSelect"
                  :value="item.value"
                  :key="item.value"
                  :label="item.displayValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="店铺Logo" prop="logo">
              <UploadImage
                v-if="dialog.edit"
                action="/goods-shop/admin/upload-img"
                formProp="logo"
                :fileList="[{url: form.logo ? form.logo.indexOf('http') >= 0 ? form.logo: resUrl + form.logo: ''}]"
                @imgUploadSuccess="imgUploadSuccess"
                @imgRemoveSuccess="imgRemoveSuccess"
              />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="店铺标题" prop="title">
              <el-input v-model="form.title" placeholder="请输入店铺标题" clearable />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="联系电话" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入联系电话" clearable />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
            <el-form-item label="店铺地址" prop="address">
              <el-input type="textarea" v-model="form.address" placeholder="请输入店铺地址" clearable />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="经纬度" prop="longitude">
              <el-button
                type="text"
                size="small"
                @click="showBaiduMap({
                searchDialog: 'UserMain'
              })"
              >选择经纬度 :</el-button>
              {{this.form.longitude}} - {{this.form.latitude}}
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
            <el-form-item label="店铺简介" prop="intro">
              <el-input type="textarea" v-model="form.intro" placeholder="请输入店铺简介" clearable />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="店铺等级" prop="level">
              <el-input-number
                v-model="form.level"
                placeholder="请输入店铺等级"
                clearable
                style="width: 100%;"
              />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="审核状态" prop="checkStatus">
              <el-select
                v-model="form.checkStatus"
                placeholder="请选择审核状态"
                clearable
                style="width: 100%;"
              >
                <el-option
                  v-for="item in selects.checkStatusSelect"
                  :value="item.value"
                  :key="item.value"
                  :label="item.displayValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
            <el-form-item label="审核描述" prop="checkDescription">
              <el-input
                type="textarea"
                v-model="form.checkDescription"
                placeholder="请输入审核描述"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="审核人手机号" prop="checkedUserPhone">
              <el-input v-model="form.checkedUserPhone" placeholder="请输入审核人手机号" disabled clearable />
              <el-button
                type="text"
                size="small"
                @click="showJoinSearch({
                searchDialog: 'UserMain'
              })"
              >选择审核人手机号</el-button>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="审核时间" prop="checkedTime">
              <el-date-picker
                type="datetime"
                v-model="form.checkedTime"
                placeholder="请输入审核时间"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss"
                style="width: 100%;"
                clearable
              ></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer">
        <el-button @click="dialog.edit = false">取消</el-button>
        <el-button type="primary" @click="edit" :loading="loading.edit">修改</el-button>
      </div>
    </el-dialog>
    <!-- 百度地图选择经纬度 -->
    <BaiduMapDialog ref="baiduMapDialog" v-on:confirmLngAndLat="confirmLngAndLat" />
  </div>
</template>

<script>
import * as utils from '@/utils/utils'
import * as ResponseStatus from '@/utils/response-status'
import UploadImage from '@/components/Upload/UploadImage'
import Tinymce from '@/components/Tinymce'
import BaiduMapDialog from '@/components/BaiduMapDialog'
const resUrl = process.env.VUE_APP_RES_URL
export default {
  name: 'GoodsShopAddEdit',
  components: {
    UploadImage,
    Tinymce,
    BaiduMapDialog,
  },
  data() {
    return {
      dialog: {
        add: false,
        edit: false,
      },
      loading: {
        add: false,
        edit: false,
      },
      form: {
        userId: undefined,
        userPhone: undefined,
        userNickname: undefined,
        userIsActive: undefined,
        categoryId: undefined,
        categoryTitle: undefined,
        categoryIsActive: undefined,
        subjectType: undefined,
        logo: undefined,
        title: undefined,
        phone: undefined,
        address: undefined,
        longitude: undefined,
        latitude: undefined,
        intro: undefined,
        level: undefined,
        checkStatus: undefined,
        checkDescription: undefined,
        checkedUserId: undefined,
        checkedUserPhone: undefined,
        checkedTime: undefined,
      },
      validateRules: {
        userId: [
          {
            type: 'integer',
            required: true,
            message: '此项为必须项',
            trigger: 'blur, change',
          },
        ],
        userPhone: [
          {
            type: 'string',
            required: true,
            message: '此项为必须项',
            trigger: 'blur',
          },
          {
            type: 'string',
            min: 1,
            max: 11,
            message: '必须1-11个字符',
            trigger: 'blur',
          },
        ],
        userNickname: [
          {
            type: 'string',
            required: true,
            message: '此项为必须项',
            trigger: 'blur',
          },
          {
            type: 'string',
            min: 1,
            max: 50,
            message: '必须1-50个字符',
            trigger: 'blur',
          },
        ],
        userIsActive: [
          {
            type: 'integer',
            required: true,
            message: '此项为必须项',
            trigger: 'blur, change',
          },
        ],
        categoryId: [
          {
            type: 'integer',
            required: true,
            message: '此项为必须项',
            trigger: 'blur, change',
          },
        ],
        categoryTitle: [
          {
            type: 'string',
            required: true,
            message: '此项为必须项',
            trigger: 'blur',
          },
          {
            type: 'string',
            min: 1,
            max: 20,
            message: '必须1-20个字符',
            trigger: 'blur',
          },
        ],
        categoryIsActive: [
          {
            type: 'integer',
            required: true,
            message: '此项为必须项',
            trigger: 'blur, change',
          },
        ],
        subjectType: [
          {
            type: 'integer',
            required: true,
            message: '此项为必须项',
            trigger: 'blur, change',
          },
        ],
        logo: [
          {
            type: 'string',
            min: 1,
            max: 500,
            message: '必须1-500个字符',
            trigger: 'blur',
          },
        ],
        title: [
          {
            type: 'string',
            required: true,
            message: '此项为必须项',
            trigger: 'blur',
          },
          {
            type: 'string',
            min: 1,
            max: 50,
            message: '必须1-50个字符',
            trigger: 'blur',
          },
        ],
        phone: [
          {
            type: 'string',
            min: 1,
            max: 200,
            message: '必须1-200个字符',
            trigger: 'blur',
          },
        ],
        address: [
          {
            type: 'string',
            min: 1,
            max: 255,
            message: '必须1-255个字符',
            trigger: 'blur',
          },
        ],
        intro: [
          {
            type: 'string',
            required: true,
            message: '此项为必须项',
            trigger: 'blur',
          },
          {
            type: 'string',
            min: 1,
            max: 255,
            message: '必须1-255个字符',
            trigger: 'blur',
          },
        ],
        checkDescription: [
          {
            type: 'string',
            min: 1,
            max: 255,
            message: '必须1-255个字符',
            trigger: 'blur',
          },
        ],
        checkedUserPhone: [
          {
            type: 'string',
            min: 1,
            max: 11,
            message: '必须1-11个字符',
            trigger: 'blur',
          },
        ],
      },
      dataDictCodes: ['YES_NO', 'SUBJECT_TYPE', 'SHOP_CHECK_STATUS'],
      selects: {
        userIsActiveSelect: [],
        categoryIsActiveSelect: [],
        subjectTypeSelect: [],
        checkStatusSelect: [],
      },
      resUrl: resUrl,
    }
  },
  computed: {},
  mounted() {
    this.initDataDict()
  },
  methods: {
    initDataDict() {
      utils
        .getDataDict(this.dataDictCodes)
        .then((response) => {
          if (response) {
            utils.getSelectDictItems({
              mainComponent: this,
              dataDictItems: response,
              dictCode: 'YES_NO',
              formProp: 'userIsActive',
              dataProp: 'userIsActiveSelect',
            })
            utils.getSelectDictItems({
              mainComponent: this,
              dataDictItems: response,
              dictCode: 'YES_NO',
              formProp: 'categoryIsActive',
              dataProp: 'categoryIsActiveSelect',
            })
            utils.getSelectDictItems({
              mainComponent: this,
              dataDictItems: response,
              dictCode: 'SUBJECT_TYPE',
              formProp: 'subjectType',
              dataProp: 'subjectTypeSelect',
            })
            utils.getSelectDictItems({
              mainComponent: this,
              dataDictItems: response,
              dictCode: 'SHOP_CHECK_STATUS',
              formProp: 'checkStatus',
              dataProp: 'checkStatusSelect',
            })
          }
        })
        .catch((error) => {})
    },
    dialogClosed(formRef) {
      this.$refs[formRef].resetFields()
      for (let key in this.form) {
        this.form[key] = undefined
      }
    },
    add() {
      this.$emit('add')
    },
    edit() {
      this.$emit('edit')
    },
    /** 触发父组件展示关联表数据的搜索对话框 */
    showJoinSearch(options) {
      this.$emit('showJoinSearchDialog', options)
    },
    imgUploadSuccess(formProp, url) {
      this.form[formProp] = url
    },
    imgRemoveSuccess(formProp) {
      this.form[formProp] = undefined
    },
    /**
     * 显示百度地图的模态窗
     */
    showBaiduMap() {
      let baiduMapDialog = this.$refs.baiduMapDialog
      if (this.form.longitude && this.form.latitude) {
        baiduMapDialog.center.lat = this.form.latitude
        baiduMapDialog.center.lng = this.form.longitude
      }
      baiduMapDialog.mapDialog = true
    },
    /**
     * 确认选择经纬度
     */
    confirmLngAndLat(lng, lat) {
      this.form.longitude = lng
      this.form.latitude = lat
    },
  },
}
</script>

<style>
</style>